<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/28 15:39
 * Desc: 会员钱包
 */
class Controller_Pc_Api_V2_Member_Bag extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Member_Bag
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Member_Bag
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('member_bag', $this->request_params);

        // 服务
        $this->service = new Service_Api_Member_Bag();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    // 提现方式
    public function action_withdraw_way()
    {
        try {
            $this->result['data'] = $this->service->withdraw_way();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 我的钱包
    public function action_wallet_index()
    {
        try {
            $this->validation->wallet_index()->check();

            $this->result['data'] = $this->service->wallet_index($this->mid);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 提现申请
    public function action_withdraw_audit()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->withdraw_audit()->check();

            $this->result['data'] = $this->service->withdraw_audit($this->mid, $this->request_params);
            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
            $db->rollback();
        }
    }

    //提现记录
    public function action_withdraw_record()
    {
        try {
            $this->validation->withdraw_record()->check();

            $this->result['data'] = $this->service->withdraw_record($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    //记录详情
    public function action_record_info()
    {
        try {
            $this->validation->record_info()->check();

            $this->result['data'] = $this->service->record_info($this->mid, $this->request_params['id']);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    //交易明细
    public function action_cash_log()
    {
        try {
            $this->validation->cash_log()->check();

            $this->result['data'] = $this->service->cash_log($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}
